﻿Imports System.IO.File

Public Class frmUpdate

    Private m_szGetNowVersion As String
    Private m_szGetFileVersion As String = ""
    Private g_szInitFileName As String

    Private m_szPath As String
    Private m_szFileName As String
    Private m_szBkpName As String
    Private m_szExe As String
    Private m_szBkp As String

    Private Structure ServerRegTYPE
        Dim szIPaddr As String
        Dim szLogin As String
        Dim szPasswd As String
        Dim szPath As String
    End Structure
    Private m_Server As ServerRegTYPE

    Private Sub frmUpdate_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        PutIniFile("SERVER", "VerPath", m_Server.szPath, g_szInitFileName)
    End Sub

    Private Sub frmUpdate_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("ICタグソフトに戻りますか？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Dim ProcId As Integer
            Try
                ProcId = Shell(m_szFileName)

            Catch ex As System.IO.FileNotFoundException
                ProcId = 0
            End Try
        Else
            'e.Cancel = True
        End If
    End Sub

    Private Sub frmUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim szCmdLine As String
        Dim cmds() As String

        '>>20171109 兪
        g_szInitFileName = My.Application.Info.DirectoryPath & "\" & "SF.ini"

        '●データベース初期値読込
        m_Server.szLogin = GetIniFile("SERVER", "UID", "atlas", g_szInitFileName)
        m_Server.szPasswd = GetIniFile("SERVER", "PAS", "atlas", g_szInitFileName)
        'm_Server.szPath = GetIniFile("SERVER", "VerPath", "SF_SOFT/", g_szInitFileName)
        m_Server.szPath = "SF_SOFT/"
        '<<20171109 兪

        '●コマンドライン引数取得
        cmds = System.Environment.GetCommandLineArgs()
        For Each szCmdLine In cmds
            If "-C" = szCmdLine.Substring(0, 2) Then
                m_szGetNowVersion = szCmdLine.Substring(2)
            End If
            If "-V" = szCmdLine.Substring(0, 2) Then
                m_szGetFileVersion = szCmdLine.Substring(2)
            End If
            If "-I" = szCmdLine.Substring(0, 2) Then
                m_Server.szIPaddr = szCmdLine.Substring(2)
            End If
        Next

        '●ファイル情報取得
        m_szPath = My.Application.Info.DirectoryPath
        m_szFileName = My.Application.Info.AssemblyName.Replace("_Update", ".exe")
        m_szBkpName = My.Application.Info.AssemblyName.Replace("_Update", ".bkp")
        m_szExe = m_szPath & "\" & m_szFileName
        m_szBkp = m_szPath & "\" & m_szBkpName

        lblCurVer.Text = m_szGetNowVersion
        lblNewVer.Text = m_szGetFileVersion

        btnUpdate.Enabled = CheckVersion()
        btnReturn.Enabled = CheckBackup()
    End Sub

    Private Function CheckVersion() As Boolean
        If String.IsNullOrEmpty(lblNewVer.Text) Then
            Return False
        ElseIf String.IsNullOrEmpty(lblCurVer.Text) Then
            If IsNumeric(lblNewVer.Text.Replace(".", "")) AndAlso CInt(lblNewVer.Text.Replace(".", "")) > 0 Then
                Return True
            Else
                Return False
            End If
        Else
            Return CalcVersion(lblNewVer.Text) > CalcVersion(lblCurVer.Text)
        End If
    End Function

    Private Function CalcVersion(ByVal szVer As String) As Long
        Dim szVerArr() As String = szVer.Split(CChar("."))
        Dim iVer As Long = 0
        For Each ver As String In szVerArr
            iVer = iVer * 10 ^ 3 + CInt(ver)
        Next
        CalcVersion = iVer
    End Function

    Private Function CheckBackup() As Boolean
        Dim files = My.Computer.FileSystem.GetFiles(m_szPath, _
                                                    FileIO.SearchOption.SearchAllSubDirectories, "*.bkp")
        Return files.Count > 0
        'Return Exists(m_szBkp)
    End Function

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            FtpConnectServer(m_Server.szIPaddr, m_Server.szLogin, m_Server.szPasswd)
            'MsgBox(m_Server.szPath)
            FtpCd(m_Server.szPath)
            Dim szLs() As String = FtpLs()
            If szLs.Length <= 0 Then
                Exit Sub
            End If
            FtpCd(lblNewVer.Text)

            'Backup
            'With My.Computer.FileSystem

            '    Dim myFiles = .GetFiles(m_szPath, _
            '                            FileIO.SearchOption.SearchAllSubDirectories, "*.exe", "*.dll")
            '    For Each szFile As String In myFiles
            '        If szFile.Replace(".exe", "").EndsWith(My.Application.Info.AssemblyName) Then
            '            'MsgBox(szFile & ":" & My.Application.Info.AssemblyName)
            '            Continue For
            '        End If
            '        Dim szBkp As String = szFile & ".bkp"
            '        Delete(szBkp)
            '        .MoveFile(szFile, szBkp)
            '    Next
            'End With

            szLs = FtpLs()

            For Each szFile As String In szLs
                If szFile.ToUpper.EndsWith(".EXE") OrElse szFile.ToUpper.EndsWith(".DLL") Then
                    With My.Computer.FileSystem
                        If .FileExists(m_szPath & "\" & szFile) Then
                            Dim szBkp As String = szFile & ".bkp"
                            Delete(m_szPath & "\" & szBkp)
                            .MoveFile(m_szPath & "\" & szFile, m_szPath & "\" & szBkp)
                        End If
                    End With
                    'MsgBox(FtpPwd() & "/" & szFile & "-->" & m_szPath & "\" & szFile)
                    FtpGet(m_szPath & "\" & szFile, FtpPwd() & "/" & szFile)
                End If
            Next

            'フォルダー対応
            For Each szFolder As String In szLs
                If Not szFolder.Contains(".") AndAlso szFolder.EndsWith("/") Then
                    With My.Computer.FileSystem
                        'MsgBox(szFile)
                        Dim szPath As String = m_szPath & "\" & szFolder.Replace("/", "\")
                        If .DirectoryExists(szPath) Then
                            .CreateDirectory(szPath)
                        End If
                        FtpCd(szFolder)
                        Dim szLsLs = FtpLs()
                        For Each subFoldFile As String In szLsLs
                            If subFoldFile.ToUpper.EndsWith(".EXE") OrElse subFoldFile.ToUpper.EndsWith(".DLL") Then
                                Dim szFile As String = szPath & subFoldFile
                                If .FileExists(szFile) Then
                                    Dim szBkp As String = szFile & ".bkp"
                                    Delete(szBkp)
                                    .MoveFile(szFile, szBkp)
                                End If
                                'MsgBox(FtpPwd() & "/" & subFoldFile & "-->" & szFile)
                                FtpGet(szFile, FtpPwd() & "/" & subFoldFile)
                            End If

                        Next
                        FtpCd("../")
                    End With
                End If
            Next


        Catch ex As Exception
            MsgBox(ex.Message)
            btnReturn_Click(btnReturn, New EventArgs)
        End Try
        Me.Close()
    End Sub

    Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
        btnReturn.Enabled = False
        Try
            With My.Computer.FileSystem
                Dim bkpFiles = .GetFiles(m_szPath, _
                                    FileIO.SearchOption.SearchAllSubDirectories, "*.bkp")
                For Each szBkp As String In bkpFiles
                    If szBkp.Replace(".exe", "").EndsWith(My.Application.Info.AssemblyName) Then
                        'MsgBox(szBkp & ":" & My.Application.Info.AssemblyName)
                        Continue For
                    End If
                    Dim szRet As String = szBkp.Replace(".bkp", "")
                    'MsgBox(szBkp & "-->" & szRet)
                    Delete(szRet)
                    .MoveFile(szBkp, szRet)
                Next
            End With
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Me.Close()
        End Try
    End Sub

End Class

